<?php

namespace app\controller;


use app\service\GoodsService;
use const support\CORRECT;
use const support\ERROR;
use const support\LIMIT;
use const support\OPERATE_ERROR;
use const support\PAGE;

class GoodsController
{

    //商品列表
    public function lists(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'page'	=>PAGE,
            'limit'	=>LIMIT,
            'cate_id'=>0
        ]);
        try {
            $data=GoodsService::getLists($params['page'],$params['limit'],$params['cate_id']);
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //商品详情
    public function details(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'goods_id'	=>0
        ]);
        try {
            $data=GoodsService::getDetails(getMemberId(),$params['goods_id']);
            return buildSuccess($data);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //关注/取消关注商品
    public function goodsConcern(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'goods_id'	=>0,
            'is_concern'=>CORRECT
        ]);
        try {
            $ret=GoodsService::toGoodsConcernStatus(getMemberId(),$params['goods_id'],$params['is_concern']);
            if (!$ret){
                $msg=($params['is_concern']==CORRECT)?'关注失败':'取消关注失败';
                return buildFail(OPERATE_ERROR,$msg);
            }
            return buildSuccess();
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //商品活动日历商品
    public function goodsEventCalendar(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'year'   =>'',
            'month'	 =>'',
            'day'    =>''
        ]);
        try {
            //进行数据整理
            $monthTime=strtotime(sprintf('%s-%s-01',$params['year'],$params['month']));
            $dayTime=strtotime(sprintf('%s-%s-%s',$params['year'],$params['month'],$params['day']));
            $info=GoodsService::activityGoodsList($monthTime,$dayTime);
            return buildSuccess($info);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

    //商品活动日历事件
    public function goodsActivityEvent(): \support\Response
    {
        //接收前端传参
        $params=getRequestData([
            'year'   =>'',
            'month'	 =>'',
        ]);
        try {
            $monthTime=strtotime(sprintf('%s-%s-01',$params['year'],$params['month']));
            $info=GoodsService::getGoodsActivityEventDate($monthTime);
            return buildSuccess($info);
        }catch (\Throwable $e){
            return buildFail(ERROR,$e->getMessage());
        }
    }

}